package service;

import java.util.List;

import models.Model;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import utils.Utils;
import dao.IStoreDao;
import dao.entity.Store;

@Service
public class StoreService {

	private static final Logger LOGGER = Logger.getLogger(StoreService.class);
	private IStoreDao storeDao;
	private Utils<Store> utils;

	@Autowired
	public void setUtils(Utils<Store> utils) {
		this.utils = utils;
	}

	@Autowired
	public void setStoreDao(IStoreDao storeDao) {
		this.storeDao = storeDao;
	}

	public Store get(long id) {
		return storeDao.get(id);
	}

	@Transactional
	public void create(Store store) {
		LOGGER.info("Start method create. Params in: Store store");
		storeDao.save(store);
		LOGGER.info("End method create. Params in: Store store");
	}

	// @Transactional
	public void create(Model model) {
		LOGGER.info("Start method create. Params in: Model model");
		if (utils.convertModelToEntity(model) != null) {
			storeDao.saveOrUpdate(utils.convertModelToEntity(model));
		}
		LOGGER.info("End method create. Params in: Model model");
	}

	@Transactional
	public void createOrUpdate(Store store) {
		storeDao.saveOrUpdate(store);
	}

	public void delete(Model model) {
		if (utils.convertModelToEntity(model) != null) {
			storeDao.delete(utils.convertModelToEntity(model));
		}
	}

	public List<Store> getAll() {
		return storeDao.getAll();
	}

}
